//
//  HRRecommendFriendsViewController.m
//  Hear
//
//  Created by mac on 2017/7/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRRecommendFriendsViewController.h"
#import "HRUMShareTool.h"
#import "NSMutableAttributedString+GDAttString.h"
#import "HRSignViewModel.h"
#import "HRShowSignResultView.h"
@interface HRRecommendFriendsViewController ()
@property(nonatomic, strong)UILabel *titleLabel;
@property (nonatomic,strong)HRSignViewModel *viewModel;

@end

@implementation HRRecommendFriendsViewController
#pragma mark - Get and Set
- (HRSignViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRSignViewModel alloc] init];
    }
    return _viewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐好友";
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupView];
    
    
}

- (void)setupView{
//    UIButton *leftButton = [UIButton buttonWithType:0];
//    leftButton.size = CGSizeMake(20, 20);
//    [leftButton setImage:[UIImage imageNamed:@"Back-Arrow"] forState:UIControlStateNormal];
//    [leftButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kFullWidth-90)/2, 120, 90, 90)];
    iconImageView.image = [UIImage imageNamed:@"remendFriends"];
    iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    iconImageView.clipsToBounds = YES;
    [self.view addSubview:iconImageView];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, iconImageView.bottom +20, kFullWidth, 30)];
    titleLabel.text = @"Hearer，我听见了你的声音";
    titleLabel.textColor = MainColor;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textAlignment = 1;
    [self.view addSubview:titleLabel];
    
    
    
    UILabel *remendLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.bottom +20, kFullWidth-40, 80)];
    remendLabel.numberOfLines = 0;
    remendLabel.attributedText = [NSMutableAttributedString attStringFromTextForChatting:@"厌倦了看脸的世界？\rhearer们还不够有趣？\r就去邀请一批有趣的灵魂一起玩转Hearer吧！" textFont:[UIFont boldSystemFontOfSize:16]];
    remendLabel.textColor = FirstTextColor;
    remendLabel.font = [UIFont boldSystemFontOfSize:16];
    remendLabel.textAlignment = 1;
    [self.view addSubview:remendLabel];
    
    
    
    
    
    
    UIButton *shareButton = [UIButton buttonWithType:0];
    shareButton.frame = CGRectMake(40, kFullHeight-100, kFullWidth-80, 40);
    shareButton.layer.cornerRadius = 8;
    shareButton.clipsToBounds = YES;
    [shareButton setTitle:@"立即邀请好友" forState:0];
    [shareButton setTitleColor:[UIColor whiteColor] forState:0];
    shareButton.titleLabel.font = CustomUIFont(15);
    [shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
    shareButton.backgroundColor = MainColor;
    [self.view addSubview:shareButton];
    
    
    UILabel *hearIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, shareButton.top -30, kFullWidth, 30)];
    hearIdLabel.text = [NSString stringWithFormat:@"你的邀请码为：%@",[self transformNumber:self.hearIdStr withNumberSystem:@"8"]];
    hearIdLabel.textColor = FirstTextColor;
    hearIdLabel.font = [UIFont boldSystemFontOfSize:13];
    hearIdLabel.textAlignment = 1;
    [self.view addSubview:hearIdLabel];
}



-(NSString *)transformNumber:(NSString *)str withNumberSystem:(NSString *)sys
{
    NSMutableString *mstring = [NSMutableString stringWithFormat:@"X"];
    NSString *bitString = [NSString stringWithFormat:@"0123456789ABCDEF"];
    long long int tmp = [str intValue],num = [sys intValue], p, a, b;
    if(num ==2)
    {
        a = 1;
        b = 1;
    }else if (num == 8)
    {
        a = 7;
        b = 3;
    }else if (num == 16)
    {
        a = 15;
        b = 4;
    }else
    {
        DDLog(@"您输入的进制错误!请输入2,8,16进制!");
        return nil;
    }
    while(tmp!=0)
    {
        p=tmp&a;
        NSString *str1=[NSString stringWithFormat:@"%c",[bitString characterAtIndex:p]];
        [mstring insertString:str1 atIndex:0];
        tmp=tmp>>b;
    }
    
    return [mstring substringWithRange:NSMakeRange(0, [mstring length]-1)];
}


- (void)shareButtonClick{
    [MobClick event:@"Hearer9"];
    WeakSelf
    [HRUMShareTool uMShareToolTitle:@"Hearer我听见了你的声音" andContentStr:@"喜欢一个人，始于Hear，喜于颜值，敬于才华。来Hearer找到你的灵魂小伙伴！" andImage:[UIImage imageNamed:@"hearicon"] andShareUrl:[NSString stringWithFormat:@"%@/M/Invitation/%@",ShareHeadUrl,USERID] isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            [weakSelf saveUserShareRecord];
        }
    }];
}

- (void)saveUserShareRecord{
    WeakSelf

    [self.viewModel saveUserShareRecord:@{@"userLocalId":USERID,@"CreateTime":[self getCurrentTime]} isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            dispatch_async(dispatch_get_main_queue(), ^{
                [HRShowSignResultView showSignResultView:@"恭喜您分享成功" andGoldNum:[NSString stringWithFormat:@"+%@",weakSelf.coins]];
            });
        }
    }];
}
- (NSString *)getCurrentTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    return [formatter stringFromDate:[NSDate date]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = YES;
}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
